package com.example.administrator.reader.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.example.administrator.reader.R;
import com.example.administrator.reader.core.BaseActivity;
import com.example.administrator.reader.entity.RequestStatus;
import com.example.administrator.reader.request.BaseRequest;
import com.example.administrator.reader.request.UserRequest;
import com.example.administrator.reader.utils.Constants;
import com.example.administrator.reader.utils.MyToast;

public class RegisterActivity extends BaseActivity {

    private Button bt_register;
    private EditText et_account;
    private EditText et_password;
    private UserRequest userRequest;

    @Override
    protected void setUpContentView() {
        setContentView(R.layout.activity_register, R.string.register);
    }

    @Override
    protected void setUpView() {
        bt_register = (Button) findViewById(R.id.bt_register);
        et_account = (EditText) findViewById(R.id.et_account);
        et_password = (EditText) findViewById(R.id.et_password);
    }

    @Override
    protected void setUpData(Bundle savedInstanceState) {
        bt_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userRequest.firstLoad(Constants.getRegisterStatusUrl(et_account.getText().toString(), et_password.getText().toString()));
            }
        });

        userRequest = new UserRequest();
        userRequest.setInter(new BaseRequest.BaseInter<RequestStatus>() {
            @Override
            public void handleData(RequestStatus data) {
                if (data.getStatus().equals("success")) {
                    MyToast.showToast("注册成功");

                    Intent intent = new Intent();
                    intent.putExtra("account", et_account.getText().toString());
                    setResult(Constants.resultCode, intent);
                    finish();
                } else {
                    MyToast.showToast("账号重复");
                }
            }
        });
    }

}
